package com.example.flutter_android_screen_plugin;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import java.lang.reflect.Method;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** FlutterAndroidScreenPlugin */
public class FlutterAndroidScreenPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_android_screen_plugin");
    channel.setMethodCallHandler(this);
  }

  public static final String open = "open";
  public static final String close = "close";
  private   Activity activity;
  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    try {
      WindowManager.LayoutParams lp =  activity.getWindow().getAttributes();
      Method getInt = Class.forName("android.os.SystemProperties").getMethod("getInt", String.class, int.class);
      int notch = (int) getInt.invoke(null, "ro.miui.notch", 0);// 小米
      //  刘海屏、安卓P
      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && notch == 1) {
        switch (call.method) {
          case open:
            System.out.println("是小米,打开全屏"+String.valueOf(notch));
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            activity.getWindow().setAttributes(lp);
            result.success("open");
            break;
          case close:
            System.out.println("是小米,退出全屏 并导航栏 透明"+String.valueOf(notch));
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
            activity.getWindow().setAttributes(lp);
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
            result.success("close");
            break;
        }
      }
    }catch (Exception e){
      System.out.println(e);
      result.success(e);
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    // 拿到Activity
    this.activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
